package com.yd.mvc.pc.jewellery;

import com.yd.app.service.pc.jewellery.jewelleryPageService.WebOrderService;
import com.yd.app.service.sys.UserService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Order;
import com.yd.domain.business.OrderDetial;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.OrderParam;
import com.yd.domain.sys.PermEnum;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * Created by sunxianhui on 2018/5/14.
 */
@RestController
@RequestMapping(value = "/api/webOrder")
public class WebOrderController {
    @Autowired
    private WebOrderService webOrderService;
    @Autowired
    private UserService userService;
    /**
     * 添加新订单
     * @param orderDetial
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Order addOrder(@RequestBody OrderDetial orderDetial) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        User u=userService.getUserById(user.getUserId());
        Order order=null;
        if(u!=null){
            order=webOrderService.addOrderDetail(orderDetial,u.getId(),u.getCompanyName(),u.getRealName());

        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
        return order;
    }

    /**
     * 根据条件分页查询
     * @param orderParam
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getOrderList(@RequestBody OrderParam orderParam, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) throws BusinessException {
        AuthUtil.AuthedUser u = AuthUtil.getCurrentUser();
        orderParam.setUserId(u.getUserId());
        PageModel pageModel = null;
        if(u!=null){
            try {
                pageModel = webOrderService.getOrderList(orderParam, pageIndex, pageSize);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException(500, "获取订单信息失败");
            }
        }
        return pageModel;
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public void deleteOrder(@PathVariable("id") String id, HttpSession session) throws BusinessException {
        AuthUtil.AuthedUser u = AuthUtil.getCurrentUser();
        if(u!=null){
            if(u.getPermCodeList().contains(PermEnum.ORDER_MANAGER)) {//是否具有订单管理权限
                webOrderService.deleteOrderByid(id,PermEnum.ORDER_MANAGER);
            }else{
                webOrderService.deleteOrderByid(id,"null");
            }
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
    }

    /**
     * 根据id查询
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/getOrder/{orderId}", method = RequestMethod.GET)
    public Order getOrder(@PathVariable("orderId") String orderId) {
        Order order = webOrderService.selectOrderById(orderId);
        return order;
    }
}
